/**
 * @author Xiang.Yu
 * @version 0.0.1
 * @date 2017/4/11
 * @description ai
 */
export const action = {
    sort(tiles){
        tiles.sort((tileA, tileB) => {

            if (tileA.type !== tileB.type) {
                return tileA.type.localeCompare(tileB.type);
            } else {
                return tileA.order - tileB.order;
            }
        });
    },
};

export const reaction = {
    /**
     * 是否可以碰,返回可以碰的手牌序号
     */
    canPeng(tile,hands){
        //计算手牌相同牌
        let count = [];
        for (let i = 0; i < hands.length; i++) {
            if(hands[i].sameTo(tile)){
                count.push(i);
            }
            if(count.length===2){
                break;
            }
        }
        if(count.length>=2){
            return count;
        }else{
            return false;
        }
    },
    /**
     * 是否可以吃，返回吃牌的序号
     */
    canChi(tile,hands){
        //查询是否有一句话,例如4有3中可能 234,345,456
        let sequence = [];
        for (let i = 0; i < hands.length; i++) {
            if(hands[i].prevTo(tile)){
                //有3,再找5，或2
                for(let j=0;j<hands.length;j++){
                    if(hands[j].prevTo(hands[i])){
                        //找到2
                        sequence.push(j,i);
                        return sequence;
                    }
                    if(hands[j].nextTo(tile)){
                        sequence.push(i,j);
                        return sequence;
                    }
                }
            }
            if (hands[i].nextTo(tile)) {
                //有5，只用找6, 35已经被检查没有
                for(let j=0;j<hands.length;j++){
                    if(hands[j].nextTo(hands[j])){
                        sequence.push(i,j);
                        return sequence;
                    }
                }
            }

        }
        return false;
    },
    /**
     * 是否可以杠,返回可以碰的手牌序号
     */
    canGang(tile,hands){
        //计算手牌相同牌
        let count = [];
        for (let i = 0; i < hands.length; i++) {
            if(hands[i].sameTo(tile)){
                count.push(i);
            }
            if(count.length===3){
                break;
            }
        }
        if(count.length>=3){
            return count;
        }else{
            return false;
        }
    },
    canHu(tile,hands){

    }
};

const hu = {

};